package com.wuyan.locationdog.ui.my

import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bus.Bus
import com.arcsoft.commonbase.bus.bus_device_info_modify
import com.arcsoft.commonbase.bus.bus_mark_refresh
import com.arcsoft.commonbase.bus.bus_toast
import com.arcsoft.commonbase.ext.createEmptyView
import com.arcsoft.commonbase.widgets.dialog.CommonDialog
import com.arcsoft.commonbase.widgets.dialog.ToastBean
import com.arcsoft.commonbase.widgets.dialog.ToastPopup
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.adapter.MarkListAdapter
import com.wuyan.locationdog.databinding.TeamListActivityBinding
import com.wuyan.locationdog.viewmodel.DeviceViewModel


class MarkListActivity : BaseBindingActivity<TeamListActivityBinding>() {
    private val mViewModel: DeviceViewModel by viewModels()

    private val mAdapter by lazy { MarkListAdapter(R.layout.item_mark, null) }

    override fun initData() {

        setTitleText("标记管理")

        binding.apply {
            btnAdd.setOnClickListener {
                ActivityUtils.startActivity(MarkMapActivity::class.java)
            }

            recyclerView.apply {
                layoutManager = LinearLayoutManager(context)
                adapter = mAdapter.apply {
                    setEmptyView(
                        createEmptyView(
                            "暂无标记地点",
                            com.arcsoft.commonbase.R.mipmap.empty_default,
                            "",
                            "立即添加"
                        ){
                            btnAdd.callOnClick()
                        }
                    )

                    addChildClickViewIds(R.id.btnDelete, R.id.itemLayout)
                    setOnItemChildClickListener { adapter, view, position ->
                        if (view.id == R.id.itemLayout) {
                            val bundle = Bundle()
                            bundle.putSerializable("bean", data[position])
                            ActivityUtils.startActivity(bundle, MarkDetailActivity::class.java)
                            return@setOnItemChildClickListener
                        }
                        CommonDialog(mActivity, title = "确定删除标记？", "") {
                            val map = HashMap<String, Any>()
                            map["pointId"] = data[position].pointId
                            mViewModel.pointDelete(map)

                        }.show()
                    }


                }
            }
            btnDelete.setOnClickListener {

            }

        }
        loadNetData()
    }

    override fun loadNetData() {
        super.loadNetData()
        val map = HashMap<String, Any>()
        map["pageSize"] = 9999
        map["pageNum"] = 1
        mViewModel.pointList(map)
    }

    override fun observe() {
        super.observe()
        Bus.observe<ToastBean>(bus_toast, this) {
            loadNetData()

            ToastPopup(this, it.mTitle).showPopupWindow();
            Bus.post(bus_mark_refresh, "")

        }

        mViewModel.mPointBean.observe(this) {

            mAdapter.setNewInstance(it.content)
        }
        mViewModel.anyBean.observe(this) {
            ToastPopup(this, "删除成功").showPopupWindow();
            loadNetData()
            Bus.post(bus_mark_refresh, "")

        }
    }
}